package com.srcbox.file.adapter

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.srcbox.file.R

class LocalAppSrc(val context: Context, val appInfoList: ArrayList<ApplicationInfo>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.local_src_list, parent, false)
        return MeHolder(view)
    }

    override fun getItemCount(): Int {
        return appInfoList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        if (holder is MeHolder) {
            holder.bind(appInfoList[position])
        }
    }

    inner class MeHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(appInfo:ApplicationInfo) {
            itemView.findViewById<TextView>(R.id.local_title).text = appInfo.loadLabel(context.packageManager)
            itemView.findViewById<ImageView>(R.id.local_icon).setImageDrawable(appInfo.loadIcon(context.packageManager))
        }
    }
}